﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SaleOnlineSystem.Controls
{
    public partial class BoxThreeNavigation : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtLoginUser.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLogin.ClientID + "')");
                txtLoginPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLogin.ClientID + "')");
            }
            if (Session["Username"] != null)
            {
                string user = Session["Employee_FullName"].ToString();
                lblLoginUser.Text = user.ToUpper();
            }
            
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string loginuser = txtLoginUser.Text;
            string loginpass = txtLoginPassword.Text;
            string matkhaulogin = LIB.Converter.Encode("sha1", loginpass);
            DataTable login = DAL.DCollector.DBCollector.Instance.StoredProcedureLogin("login", loginuser, matkhaulogin);
           
            if (login.Rows.Count == 0)
            {
                Response.Redirect(Request.Url.ToString());
            }
            else
            {              
                if (Session["Username"] == null)
                {
                    Session["Employee_ID"] = login.Rows[0]["Employee_ID"].ToString();
                    Session["Employee_Email"] = login.Rows[0]["Employee_Email"].ToString();
                    Session["Employee_Avatar"] = login.Rows[0]["Employee_Avatar"].ToString();
                    Session["Employee_Address"] = login.Rows[0]["Employee_Address"].ToString();
                    Session["Employee_Phonenumber"] = login.Rows[0]["Employee_Phonenumber"].ToString();
                    Session["Employee_FullName"] = login.Rows[0]["Employee_FullName"].ToString();
                    Session["Username"] = txtLoginUser.Text;
                    Session["Password"] = txtLoginPassword.Text;

                    HttpCookie cki = new HttpCookie("CookieUser");
                    cki.Value = txtLoginUser.Text;
                    Response.Cookies.Add(cki);
                    cki.Expires = DateTime.MaxValue;
                    HttpContext.Current.Session["SHOPPING"] = null;
                    BOL.Shopping.ShoppingCart.Instance.dataItems = null;
                    Response.Redirect(Request.Url.ToString());
                }               
            }
        }

        protected void lbtThoat_Click(object sender, EventArgs e)
        {         
                if (Request.Cookies["CookieUser"] != null)
                {
                    HttpCookie cki = new HttpCookie("CookieUser");
                    cki.Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies.Add(cki);
                }
                HttpContext.Current.Session["SHOPPING"] = null;
                BOL.Shopping.ShoppingCart.Instance.dataItems = null;
                Session.RemoveAll();
                Response.Redirect("~/trangchu.htm");                    
        }
    }
}